"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const geoserver_js_1 = require("./util/geoserver.js");
const about_js_1 = __importDefault(require("./about.js"));
class LayerGroupClient {
    constructor(url, auth) {
        this.url = url;
        this.auth = auth;
    }
    async create(workspace, layerGroupName, layers, layerGroupOptions) {
        const options = {
            mode: 'SINGLE',
            layerGroupTitle: '',
            abstractTxt: '',
            bounds: {
                minx: -180,
                maxx: -90,
                miny: 180,
                maxy: 90,
                crs: 'EPSG:4326'
            },
            ...layerGroupOptions
        };
        const publishedLayers = [];
        const styles = [];
        for (const l of layers) {
            publishedLayers.push({
                '@type': 'layer',
                name: `${workspace}:${l}`,
                href: `${this.url}/workspaces/${workspace}/layers/${l}.json`
            });
            styles.push('');
        }
        const body = {
            layerGroup: {
                name: layerGroupName,
                workspace: {
                    name: workspace
                },
                publishables: {
                    published: publishedLayers
                },
                styles: {
                    style: styles
                },
                ...options
            }
        };
        const response = await fetch(`${this.url}/workspaces/${workspace}/layergroups`, {
            credentials: 'include',
            method: 'POST',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(body)
        });
        if (!response.ok) {
            const grc = new about_js_1.default(this.url, this.auth);
            if (await grc.exists()) {
                return;
            }
            else {
                const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
                throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
        const location = response.headers.get('location');
        return location;
    }
    async get(workspace, layerGroupName) {
        const response = await fetch(`${this.url}/workspaces/${workspace}/layergroups/${layerGroupName}.json`, {
            credentials: 'include',
            method: 'GET',
            headers: {
                Authorization: this.auth
            }
        });
        if (!response.ok) {
            const grc = new about_js_1.default(this.url, this.auth);
            if (await grc.exists()) {
                return;
            }
            else {
                const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
                throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
            }
        }
        return response.json();
    }
    async update(workspace, layerGroupName, layerGroupDefinition) {
        const url = `${this.url}/workspaces/${workspace}/layergroups/${layerGroupName}.json`;
        const response = await fetch(url, {
            credentials: 'include',
            method: 'PUT',
            headers: {
                Authorization: this.auth,
                'content-type': 'application/json'
            },
            body: JSON.stringify(layerGroupDefinition)
        });
        if (!response.ok) {
            const geoServerResponse = await (0, geoserver_js_1.getGeoServerResponseText)(response);
            throw new geoserver_js_1.GeoServerResponseError(null, geoServerResponse);
        }
    }
}
exports.default = LayerGroupClient;
